﻿@using System.Data
@{
    ViewBag.Title = "购物车";
    Layout = "~/Areas/Mall/Views/Shared/MallLayout1.cshtml";
   var table= ViewData["shopcarlist"] as DataTable;
}


<div class="top_c">
    <img src="/Images/gou0.png" zt="0" />
    <label>&nbsp;全选</label>
    <div>删除</div>
</div>
<div class="spg_b">
    <label id="totalprice">合计：0元</label>
    <div onclick="jiesuan()">立即结算</div>
</div>
@{
    foreach (DataRow row in table.Rows)
    {
        var price = 0.0;
        if (row["tcname"].ToString() != "")
        {
            price = Convert.ToDouble(row["tcprice"]);
        }
        else if (row["DiscountPrice"].ToString() != "")
        {
            if (Convert.ToDouble(row["DiscountPrice"].ToString()) > 0)
            {
                price = Convert.ToDouble(row["DiscountPrice"]);
            }
            else
            {
                price = Convert.ToDouble(row["Price"]);
            }
        }
        else
        {
            price = Convert.ToDouble(row["Price"]);
        }
        <div class="carlist" id="t-@row["Id"]">
            <div class="scartool">
                <img src="/Images/gou1.png" cid="0" scid="@row["Id"]" />
                <label scid="@row["Id"]">删除</label>
            </div>
            <img src="@row["Img1"]" class="scarimg" />
            <div class="car_item">
                <p>@row["Name"]</p>
                @{
        if (row["tcname"].ToString()!= "")
        {
            <p>@row["tcname"] @row["tcvalue"] ￥@Convert.ToDouble(row["tcprice"])</p>
        }
                }
                <p price="@price" id="p-@row["Id"]">小计：@Convert.ToDouble(price * Convert.ToInt32(row["Count"])) 元</p>
                <div class="sc_count">
                    <div class="jjsf" onclick="jia('t-@row["Id"]')">+</div>
                    <div class="jjsf_num" maxcount="@Convert.ToInt32((Convert.ToInt32(row["Count1"]) - Convert.ToInt32(row["PayCountTrue"])))" id="num-@row["Id"]" >@row["Count"]</div>
                    <div class="jjsf" onclick="jian('t-@row["Id"]')">-</div>
                </div>
            </div>
        </div>
    }
}
<div style="width: 100%;height: 150px;"></div>
<script>
    $(function() {
        totalprice();
    });

    function totalprice() {
        var price = 0.0;
        $(".carlist").each(function() {
            var id = $(this).attr("id").split('-')[1];
            if ($(this).find("img:eq(0)").attr("src") == "/Images/gou0.png") {
                price += (parseFloat($("#p-" + id).attr("price")) * parseFloat($("#num-" + id).html()));
            }
        });
        $("#totalprice").html("合计：" + price + "元");
    }

    $(".top_c img").click(function() {
        if ($(".top_c img").attr("zt") == "0") {
            $(".scartool img").attr("src", "/Images/gou0.png");
            $(".top_c img").attr("zt", "1");
            $(".scartool img").each(function() {
                $(this).attr("cid", "1");
            });

        } else {
            $(".scartool img").attr("src", "/Images/gou1.png");
            $(".top_c img").attr("zt", "0");
            $(".scartool img").each(function() {
                $(this).attr("cid", "0");
            });
        }
        totalprice();
    });

    $(".top_c div").click(function() {
        if (confirm("确定删除购物车里的商品吗？")) {
            var temp = "";
            $(".scartool img").each(function() {
                if ($(this).attr("cid") == "1") {
                  temp+=$(this).attr("scid")+",";
                }
            });
            if (temp == "") {
                alert("请选择您要删除的宝贝！");
                return false;
            }
            temp = temp.substring(0, temp.length - 1);
            $.post("@Url.Action("DelShopCar")", { id: temp }, function (row) {
                if (row > 0) {
                    location.reload();
                } else {
                    alert("删除失败！");
                }
            });
        }
    });

    $(".scartool img").click(function() {

        if ($(this).attr("cid") == "0") {
            $(this).attr("src", "/Images/gou0.png");
            $(this).attr("cid", "1");
        } else {
            $(this).attr("src", "/Images/gou1.png");
            $(this).attr("cid", "0");
        }
        totalprice();
    });
    $(".scartool label").click(function() {
        var id = $(this).attr("scid");
        if (confirm("你确定删除该宝贝吗？")) {
            $.post("@Url.Action("DelShopCar")",{id:id},function(row) {
                if (row > 0) {
                    location.reload();
                } else {
                    alert("删除失败！");
                }
            });
        }
    });

    function jia(id) {
        var str1 = "#p-" + id.split('-')[1];
        var price = $(str1).attr("price");
        var str = "#num-" + id.split('-')[1];
        var count = $(str).html();
        var maxcount = $(str).attr("maxcount");
        if (parseFloat(count) < parseFloat(maxcount)) {
            var pr = (parseFloat(price) * (parseFloat(count) + 1));
            $(str1).html("小计：" + pr + "元");
            $(str).html(parseFloat(count) + 1);
            $.post("@Url.Action("EditShopCarCount")", { id: id.split('-')[1], count: parseFloat(count) +1 }, function () {
               
            });
        }
        totalprice();
    }

    function jian(id) {
        var str1 = "#p-" + id.split('-')[1];
        var price = $(str1).attr("price");

        var str = "#num-" + id.split('-')[1];
        var count = $(str).html();

        if (parseFloat(count) > 1) {

            var pr = (parseFloat(price) * (parseFloat(count) - 1));
            $(str1).html("小计：" + pr + "元");
            $(str).html(parseFloat(count) - 1);
            $.post("@Url.Action("EditShopCarCount")", { id: id.split('-')[1], count: parseFloat(count) - 1 }, function () {
               
           });
        }
        totalprice();
    }

    ///结算
    function jiesuan() {
        var ids = "";
        $(".carlist").each(function() {
            var id = $(this).attr("id").split('-')[1];
            if ($(this).find("img:eq(0)").attr("src") == "/Images/gou0.png") {
                ids += id + ",";
            }          
        });
        ids = ids.substring(0, ids.length - 1);
        if (ids != "") {
            $.post("@Url.Action("AddOrder")", { ids: ids }, function(row) {
            if (row > 0) {
                location.href = "/Mall/order/OrderList?sid=1";
            } else {
                location.reload();
            }
        });
    } else {
                alert("请选择结算的商品！");
    return false;
    }
          
    }
</script>